package cn.ysym.dao;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import cn.ysym.model.OfflineSum;

public class OfflineSumDao extends DaoBase {

    public OfflineSumDao(Context context) {
        super(context);
    }

    public List<OfflineSum> getList() {
        List<OfflineSum> ret = new ArrayList<>();
        List<String[]> objs = this.query(1000, "select id, account, procType, procDesc, procNum, upSuccNum, upFailNum from offline_sum order by id desc");
        for (String[] row : objs) {
            OfflineSum model = new OfflineSum();
            model.setId(Integer.parseInt(row[0]));
            model.setAccount(row[1]);
            model.setProcType(row[2]);
            model.setProcDesc(row[3]);
            model.setProcNum(Integer.parseInt(row[4]));
            model.setUpSuccNum(Integer.parseInt(row[5]));
            model.setUpFailNum(Integer.parseInt(row[6]));
            ret.add(model);
        }

        return ret;
    }

    public OfflineSum getLast() {
        List<String[]> objs = this.query(1, "select id, account, procType, procDesc, procNum, upSuccNum, upFailNum from offline_sum order by id desc");
        if (objs.size() > 0) {
            String[] row = objs.get(0);
            OfflineSum model = new OfflineSum();
            model.setId(Integer.parseInt(row[0]));
            model.setAccount(row[1]);
            model.setProcType(row[2]);
            model.setProcDesc(row[3]);
            model.setProcNum(Integer.parseInt(row[4]));
            model.setUpSuccNum(Integer.parseInt(row[5]));
            model.setUpFailNum(Integer.parseInt(row[6]));
            return model;
        } else return null;
    }

    public OfflineSum getById(Integer id) {
        List<String[]> objs = this.query(1, "select id, account, procType, procDesc, procNum, upSuccNum, upFailNum from offline_sum where id = ?", String.valueOf(id));
        if (objs.size() > 0) {
            String[] row = objs.get(0);
            OfflineSum model = new OfflineSum();
            model.setId(Integer.parseInt(row[0]));
            model.setAccount(row[1]);
            model.setProcType(row[2]);
            model.setProcDesc(row[3]);
            model.setProcNum(Integer.parseInt(row[4]));
            model.setUpSuccNum(Integer.parseInt(row[5]));
            model.setUpFailNum(Integer.parseInt(row[6]));
            return model;
        } else return null;
    }

    public void insert(OfflineSum model) {
        this.execSql("insert into offline_sum(account, procType, procDesc, procNum, upSuccNum, upFailNum) values(?, ?, ?, ?, ?, ?)", model.getAccount(), model.getProcType(), model.getProcDesc(), model.getProcNum(), model.getUpSuccNum(), model.getUpFailNum());
    }

    public void update(OfflineSum model) {
        this.execSql("update offline_sum set procNum = ?, upSuccNum = ?, upFailNum = ? where id = ?", model.getProcNum(), model.getUpSuccNum(), model.getUpFailNum(), model.getId());
    }

    public void deleteById(Integer id) {
        this.execSql("delete from offline_stock where sumId = ?", id);
        this.execSql("delete from offline_shipping where sumId = ?", id);
        this.execSql("delete from offline_sum where id = ?", id);
    }

    public void deleteAll() {
        this.execSql("delete from offline_stock");
        this.execSql("delete from offline_shipping");
        this.execSql("delete from offline_sum");
    }

}
